package zozopo.warehouse;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class GetGoodsInfoAction extends ActionSupport implements Action,SessionAware,ServletRequestAware,ServletResponseAware{
		//传入参数
		private String searchStr = "";
		//分页参数
		private int beginPageNum = 0;
		private int pageSize = 10;
		private int rowNum = 0;
		//传出参数
		private List<GoodsTB> goodsList;
		Map<Integer,List<PurchaseTB>> warehouseInfoMapList;
	

		//传出参数
		HttpServletRequest request;
		HttpServletResponse response;
		private Map<Object, Object> session;
		
		public void setServletRequest(HttpServletRequest arg0) {
			// TODO Auto-generated method stub
			request = arg0;
		}

		public void setSession(Map arg0) {
			// TODO Auto-generated method stub
			session = arg0;
		}

		

		public void setServletResponse(HttpServletResponse arg0) {
			// TODO Auto-generated method stub
			response = arg0;
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/xml; charset=utf-8");
		}
		
		public String execute(){
			//权限处理
			UserTB user = (UserTB) session.get("userInfo");
			if(user == null){
				return NONE;
			} 
		
			
			
			GoodsOS goodsOS = new GoodsOS();
			PurchaseOS purchaseOS = new PurchaseOS(goodsOS.getConn());
			//UserOS userOS = new UserOS();
			
			if(searchStr.equals("")){
				
				
				goodsList = goodsOS.selectGoods(beginPageNum, pageSize);
				rowNum = goodsOS.getRowNum();
				
				warehouseInfoMapList = purchaseOS.getWarehouseInfoMapList(goodsList);
			}else{
				goodsList = goodsOS.selectBySearchStr(searchStr,beginPageNum, pageSize);
				rowNum = goodsOS.getRowNumForSearchStr(searchStr);
				
				warehouseInfoMapList = purchaseOS.getWarehouseInfoMapList(goodsList);
			}
			
			//purchaseOS.releaseConnection();
			goodsOS.releaseConnection();
			
			
			
			try {
				
				response.getWriter().write("<table>");
				for(GoodsTB goods : goodsList){
					response.getWriter().write("<tr><td width='305'>");
					response.getWriter().write(goods.getName_goods());
					
					response.getWriter().write("</td><td width='105'>");
					for(PurchaseTB purchase : warehouseInfoMapList.get(goods.getId_goods())){
						response.getWriter().write(purchase.getWarehousename_purchase()  + ":" + purchase.getReserve_purchase() + "</BR>");
						
					}
					response.getWriter().write("</td></tr>");	
				}
				
				response.getWriter().write("</table>");
				
				response.getWriter().flush();
				response.getWriter().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			return NONE;
		}

		public String getSearchStr() {
			return searchStr;
		}

		public void setSearchStr(String searchStr) throws UnsupportedEncodingException {
			this.searchStr = java.net.URLDecoder.decode(searchStr, "UTF-8");
			this.searchStr = this.searchStr.trim();
		}

		public int getBeginPageNum() {
			return beginPageNum;
		}

		public void setBeginPageNum(int beginPageNum) {
			this.beginPageNum = beginPageNum;
		}

		public int getPageSize() {
			return pageSize;
		}

		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}

		public int getRowNum() {
			return rowNum;
		}

		public void setRowNum(int rowNum) {
			this.rowNum = rowNum;
		}

		public List<GoodsTB> getGoodsList() {
			return goodsList;
		}

		public void setGoodsList(List<GoodsTB> goodsList) {
			this.goodsList = goodsList;
		}

		public Map<Integer, List<PurchaseTB>> getWarehouseInfoMapList() {
			return warehouseInfoMapList;
		}

		public void setWarehouseInfoMapList(
				Map<Integer, List<PurchaseTB>> warehouseInfoMapList) {
			this.warehouseInfoMapList = warehouseInfoMapList;
		}

		
}
